
//
//  MenuProvinceView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/29.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MenuProvinceView.h"
#import "MenuProvinceTableView.h"
#import "MenuCenterCell.h"
#import "UIView+ZF.h"
#import "ProviceModel.h"
#import "CityModel.h"
#import "HomeModel.h"
#define kRowH 44
@interface MenuProvinceView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) MenuProvinceTableView *menuProvinceTabView;
@property(nonatomic,strong) NSMutableArray *cityMutb;
@property(nonatomic,assign) NSInteger row;
@end
@implementation MenuProvinceView
static NSString *identifierMenuCenterCell = @"menuCenterCell";
-(NSMutableArray *)proviceMutb
{
    if (!_proviceMutb) {
        _proviceMutb = [NSMutableArray array];
        NSString *filePatch = [[NSBundle mainBundle]pathForResource:@"ProvincesAndCities.plist" ofType:nil];
        NSArray *temp = [NSArray arrayWithContentsOfFile:filePatch];
        //        XLog(@"temp:%@",temp);
        for (NSDictionary *dict in temp) {
            ProviceModel *proviceModel = [ProviceModel mj_objectWithKeyValues:dict];
            [_proviceMutb addObject:proviceModel];
            
        }
        XLog(@"self.proviceMutb:%@",self.proviceMutb);
    }
    return _proviceMutb;
}
-(NSMutableArray *)cityMutb
{
    if (!_cityMutb) {
        _cityMutb = [NSMutableArray array];
    }
    return _cityMutb;
}
- (MenuProvinceTableView *)menuProvinceTabView
{
    if (!_menuProvinceTabView) {
        CGRect frame = CGRectMake(0.f, 0, self.w, self.h);
        _menuProvinceTabView = [[MenuProvinceTableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _menuProvinceTabView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _menuProvinceTabView.delegate = self;
        _menuProvinceTabView.dataSource = self;
        _menuProvinceTabView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
        [_menuProvinceTabView registerNib:[UINib nibWithNibName:@"MenuCenterCell" bundle:nil] forCellReuseIdentifier:identifierMenuCenterCell];
    }
    return _menuProvinceTabView;
}
- (void)setH:(CGFloat)h
{
    _h = h;
    _menuProvinceTabView.h = h;
}
- (void)setW:(CGFloat)w
{
    _w = w;
    _menuProvinceTabView.w = w;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kOrangeBg;
        [self addSubview:self.menuProvinceTabView];
    }
    return self;
}
- (void)reloadData
{
    [self.menuProvinceTabView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.proviceMutb.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    MenuCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierMenuCenterCell forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:247/255.0 green:247/255.0 blue:247/255.0 alpha:1];
    ProviceModel *proviceModel = self.proviceMutb[indexPath.row];
    cell.provinceShow = self.provinceShow;
    cell.proviceModel = proviceModel;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kRowH;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    MenuCell *cell = [tableView cellForRowAtIndexPath:indexPath];
//    self.btn = (MCButton *)[self viewWithTag:self.index];
//    HomeModel *model = self.dataArray[indexPath.row];
//    [self.btn setTitle:model.content forState:UIControlStateNormal];
//    [self resetStatus:self.btn];
//    self.btn.selected = NO;
//    [self.btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
//    model.show = YES;
//    if (self.preRow != indexPath.row) {
//        HomeModel *preModel = self.dataArray[self.preRow];
//        preModel.show = NO;
//
//    }
//    self.preRow = indexPath.row;
//    [self hideListViewAnimation];
    ProviceModel *proviceModel = self.proviceMutb[indexPath.row];
    proviceModel.show = YES;
    if (self.returnValueBlock) {
        self.returnValueBlock(indexPath.row, proviceModel.State);
    };
    ProviceModel *proviceModelPre = self.proviceMutb[self.row];
    if (self.row != indexPath.row) {
        proviceModelPre.show = NO;
    }
    self.row = indexPath.row;
    [self.menuProvinceTabView reloadData];
}
@end
